package com.es.servlet;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 描述:
 * es-servlet-
 *
 * @author Derek
 * 创建时间：2022-04-21 10:31
 */
@WebServlet(urlPatterns = "/a")
public class AServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //
        System.out.println("我是Aservlet");
        //进行请求分派，转发
        //获取请求分派器,对 /b 资源的
        RequestDispatcher dispatcher = req.getRequestDispatcher("/b");
        //进行跳转
        String flag = "你好";
        //传值规则： 使用最小的范围
        req.setAttribute("flag", flag);
        //通过请求获取session
        HttpSession session = req.getSession();
        //通过session设置属性
        session.setAttribute("name", "小马");
        //获取servletContext
        ServletContext application = this.getServletContext();
        application.setAttribute("name", "李健");
        dispatcher.forward(req,resp);
    }
}
